//
//  BKHuanJingViewController.m
//  baokaodaxue
//
//  Created by dangyangyang on 15/6/2.
//  Copyright (c) 2015年 宏图教育. All rights reserved.
//

#import "BKHuanJingViewController.h"
#import <UIImageView+AFNetworking.h>
#import "BKColleageHeaderView.h"
#import <MBProgressHUD.h>
#import "BKHistoryButton.h"
#import "BKHeZhaoCollectionViewCell.h"
#import "BKHuanJingLayout.h"
#import "BKPhotoView.h"

static NSString *huanjing = @"huanjing";
@interface BKHuanJingViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UICollectionView *photoCollectionView;

@property (nonatomic ,copy) NSArray *dataArray;
@property (weak, nonatomic) IBOutlet BKColleageHeaderView *heaerView;

@end

@implementation BKHuanJingViewController
- (IBAction)p_back:(id)sender {
    [_delegate dismiss];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _dataArray = [NSArray array];
    // Do any additional setup after loading the view from its nib.
    
  
    _nameLabel.text= _name;
    __weak BKHuanJingViewController *weakSelf = self;
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:hud];
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.labelText = @"正在加载";
    [hud show:YES];
    
    [BKColleageViewModel huanjingWithCid:_cid type:@"0" success:^(NSArray *array) {
        [hud hide:YES];
        weakSelf.dataArray = array;
    } failure:^(NSString *message) {
        [hud hide:YES];
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"无数据" delegate:nil
                                                  cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        [alertView show];
    }];
    
    [_photoCollectionView registerNib:[UINib nibWithNibName:NSStringFromClass([BKHeZhaoCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:huanjing];
    BKHuanJingLayout *layout = [[BKHuanJingLayout alloc] init];
    [_photoCollectionView setCollectionViewLayout:layout];
    
    BKHistoryButton *btn = [self.heaerView.subviews firstObject];
    btn.selected = YES;
    
}
- (IBAction)p_selecteType:(id)sender {
    __weak BKHuanJingViewController *weakSelf = self;
    UIButton *button = (UIButton *)sender;
    
    for (id sub in [_heaerView subviews]) {
        if ([sub isKindOfClass:[UIButton class]]) {
            UIButton *typeButton = (UIButton *)sub;
            if ([typeButton isEqual:sender]) {
                typeButton.selected = YES;
            }else{
            
                typeButton.selected = NO;
            }
        }
    }
    
 
    _dataArray = @[];
    __weak BKHuanJingViewController *weakself = self;

    [BKColleageViewModel huanjingWithCid:_cid type:[NSString stringWithFormat:@"%ld",button.tag-7000] success:^(NSArray *array) {
       
        weakSelf.dataArray = array;
    } failure:^(NSString *message) {
       
        [weakself.photoCollectionView reloadData];
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:nil
                                                  cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        [alertView show];
    }];

}
- (void)setDataArray:(NSArray *)dataArray{

 
    _dataArray = dataArray;
    [_photoCollectionView reloadData];
//    __weak BKHuanJingViewController *weakself = self;
//    dispatch_sync(dispatch_get_main_queue(), ^{
//        
//        
//    });
//    if ([_dataArray count]>0) {
//        for (int i=0; i<[dataArray count]; i++) {
//            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i*_scorllView.frame.size.width, 0, _scorllView.frame.size.width, _scorllView.frame.size.height)];
//            
//            NSString *urlString = [NSString stringWithFormat:@"http://www.baokaodaxue.com%@",_dataArray[i]];
//            
//            [imageView setImageWithURL:[NSURL URLWithString:urlString]];
//            
//            [_scorllView addSubview:imageView];
//        }
//        [_scorllView setContentSize:CGSizeMake(_scorllView.frame.size.width*[_dataArray count], _scorllView.frame.size.height)];
//    }else{
//    
//        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"无数据" delegate:nil
//    cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
//        [alertView show];
//    }
//    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    if ([_dataArray isKindOfClass:[NSArray class]]) {
        return [_dataArray count];
    }
    return 0;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    BKHeZhaoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:huanjing forIndexPath:indexPath];
     NSString *urlString = [NSString stringWithFormat:@"http://www.baokaodaxue.com%@",_dataArray[indexPath.row]];
    [cell.iamgeView setImageWithURL:[NSURL URLWithString:urlString]];

    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    BKHeZhaoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:huanjing forIndexPath:indexPath];
    
   
    CGRect frame = cell.frame;
    CGRect newFrame = [self.view convertRect:cell.frame fromView:collectionView];
   
   
    
    BKPhotoView *photView = [[NSBundle mainBundle]loadNibNamed:@"BKPhotoView" owner:self options:nil][0];

    
    photView.newFrame = newFrame;
    
    
    
    for (int i=0; i<[_dataArray count]; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i*self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height)];
        imageView.contentMode = UIViewContentModeScaleAspectFit;
         NSString *urlString = [NSString stringWithFormat:@"http://www.baokaodaxue.com%@",_dataArray[i]];
        [imageView setImageWithURL:[NSURL URLWithString:urlString]];
        [photView.scrollView addSubview:imageView];
        
        
    }
    photView.scrollView.pagingEnabled  = YES;
    [photView.scrollView setContentOffset:CGPointMake(indexPath.row*self.view.frame.size.width, 0)];
    
    photView.scrollView.contentSize = CGSizeMake(self.view.frame.size.width*[_dataArray count], self.view.frame.size.height);
    
    NSString *urlString = [NSString stringWithFormat:@"http://www.baokaodaxue.com%@",_dataArray[indexPath.row]];
    
    [photView.photo setImageWithURL:[NSURL URLWithString:urlString]];
    [photView setFrame:self.view.frame];
    
    
   
    [photView setFrame:newFrame];
    [self.view addSubview:photView];

    
   
    [UIView animateWithDuration:.8 animations:^{
        
        photView.scrollView.hidden = YES;
        [photView setFrame:self.view.frame];
        
    } completion:^(BOOL finished) {
       
         photView.scrollView.hidden = NO;
    }];
    
   
   
    
    
    
    
    
}
@end
